package com.feiyu.auth.controller;

import com.feiyu.auth.captcha.CaptchaBuilder;
import com.feiyu.auth.entity.AuthInfo;
import com.feiyu.auth.captcha.CaptchaInfo;
import com.feiyu.auth.granter.TokenGranter;
import com.feiyu.auth.granter.TokenGranterBuilder;
import com.feiyu.auth.granter.TokenParameter;
import com.feiyu.auth.util.Constants;
import com.feiyu.auth.util.RandomUtil;
import com.feiyu.auth.util.TokenUtil;
import com.feiyu.common.cache.CacheNames;
import com.feiyu.user.entity.User;
import com.feiyu.user.entity.UserInfo;
import com.feiyu.user.feign.UserFeignClient;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author tang ming
 * @since 2021/12/5 21:10
 */
@RestController
@RequestMapping("/auth")
@AllArgsConstructor
public class AuthController {

    private RedisUtil redisUtil;

    @Autowired
    private JavaMailSender javaMailSender;


    @PostMapping("/token")
    public R<AuthInfo>token(@RequestParam(defaultValue = "password") String grantType,
                            @RequestParam(required = false) String refreshToken,
                            @RequestParam(required = false) String account,
                            @RequestParam(required = false) String password){
        TokenParameter tokenParameter = new TokenParameter();
        tokenParameter.set("grantType",grantType)
                .set("refreshToken",refreshToken)
                .set("account",account)
                .set("password",password);
        //获取对应的授权器
        TokenGranter tokenGranter= TokenGranterBuilder.get(grantType);
        UserInfo userInfo=tokenGranter.grant(tokenParameter);
        if (userInfo==null)
            return R.fail(TokenUtil.USER_NOT_FOUND);
        return R.data(TokenUtil.createAuthInfo(userInfo));
    }

    @GetMapping("/captcha")
    public R<CaptchaInfo> captcha(){
        Captcha captcha= CaptchaBuilder.randomCaptcha(130, 48, 5);
        String verCode = captcha.text().toLowerCase();
        String key = UUID.randomUUID().toString();
        // 存入redis并设置过期时间为30分钟
        redisUtil.set(CacheNames.CAPTCHA_KEY + key, verCode, 1L, TimeUnit.MINUTES);
        // 将key和base64返回给前端
        return R.data(new CaptchaInfo(key,captcha.toBase64()));
    }

    @GetMapping("/email/captcha")
    public void sendEmail(String target){
        //判断邮箱是否存在
        String code=RandomUtil.generate(6);
        redisUtil.set(CacheNames.EMAIL_CODE+target,code,5,TimeUnit.MINUTES);
        MimeMessagePreparator mailMessage = mimeMessage -> {
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "utf-8");
            message.setFrom("tangming991@163.com", "跑路的飞鱼");
            message.setTo(target);
            message.setSubject("飞鱼客栈验证码");
            message.setSentDate(new Date());
            message.setText(Constants.html1+code+Constants.html2,true);
        };
        // 发送邮件
        javaMailSender.send(mailMessage);
    }

}
